package com.giser.testapp.lib.JavaScript;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;

import com.giser.testapp.MainActivity;
import com.giser.testapp.lib.App.GlobClass;
import com.giser.testapp.lib.Tool.EnvLog;
import com.giser.testapp.lib.Tool.FileHelper;
import com.giser.testapp.lib.Tool.ToastUtil;

import java.io.File;


/**
 * Created by lss on 2017/11/20.
 */

public class WebAppInterface {
    private Handler mHandler;
    private Context mContext;
    public WebAppInterface(Context  c,Handler handler) {
        // TODO Auto-generated constructor stub
        mContext=c;
        mHandler=handler;
    }

    //android 自带提示
    @android.webkit.JavascriptInterface
    public void sowToastShort(final String toast){
        mHandler.post(new Runnable() {
           public void run() {
                //TODO
               ToastUtil.showShort(mContext,toast);
            }
        });
    }

    /**
     * 启动相机
     */
    @android.webkit.JavascriptInterface
    public void startCameraSelector()
    {
        String sdState = Environment.getExternalStorageState();
        if(!sdState.equals(Environment.MEDIA_MOUNTED))
        {
            /*Map<String, String> params = new HashMap<String, String>();
            if(!TextUtils.isEmpty(url)){
                params.put("url", url);
            }*/
            Message msg = Message.obtain();
            msg.what = GlobClass.AlertDialog_CODE;
            //msg.obj = params;
            mHandler.sendMessage(msg);
            return;
        }
        String cameraPicturePath= FileHelper.getRandomImagePath();
        Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        camera.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
        camera.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(cameraPicturePath)));
        GlobClass.TakePic_Name=cameraPicturePath;
        ((MainActivity)mContext).startActivityForResult(camera, GlobClass.CAPTURE_IMAGE_CODE);
    }

    /**
     * 打开图库选择图片
     */
    @android.webkit.JavascriptInterface
    public void startImageSelector(){
        Intent intent=new Intent(Intent.ACTION_GET_CONTENT);//ACTION_OPEN_DOCUMENT
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("image/*");
        ((MainActivity)mContext).startActivityForResult(intent, GlobClass.SELECT_PIC_CODE);
    }

    //获得定位数据
    @android.webkit.JavascriptInterface
    public void getLocation(){
        EnvLog.i("getLocation");
        mHandler.post(new Runnable() {
            public void run() {
                //TODO
              //  EnvApplication.getLocationInfo(mContext).getLocationInfo();
                /*if(EnvApplication.getLocationInfo(mContext).start()){
                    GeographicLocation location= EnvApplication.getLocationInfo(mContext).getLocation();
                    if(location!=null){
                        BaseLocation baseLocation=new BaseLocation(location.getLatitude(),location.getLongitude());
                        Gson gson=new Gson();
                        String mess=gson.toJson(baseLocation);
                        EnvLog.i("location mess="+mess);
                        ((MainActivity)mContext).webView.loadUrl("javascript:setLoc('"+mess+"')");
                    }else{
                        EnvLog.i("location=null");
                        if(EnvApplication.getApplication().locationModuleTag==0){
                            AlertUtil.createDialog_GPS(mContext,"友情提示","定位服务未开启，请先开启定位服务！",0);
                        }

                        if(PhoneStateUtil.isGpsProviderEnabled(mContext)){
                            AlertUtil.createDialog_GPS(mContext,"友情提示","室内GPS信号弱 建议开启GPS、网络同时定位！",1);
                        }else{
                            AlertUtil.createDialog_GPS(mContext,"友情提示","定位服务未开启，请先开启定位服务！",0);
                        }
                    }
                }*/
            }
        });
    }

    //开启定位服务
    @android.webkit.JavascriptInterface
    public void startLocationService(){
        Message msg = Message.obtain();
        msg.what = GlobClass.LocationModule_CODE;
        mHandler.sendMessage(msg);
    }

    @android.webkit.JavascriptInterface
    public void startPictureService(){
        Message msg = Message.obtain();
        msg.what = GlobClass.GetPicture_CODE;
        mHandler.sendMessage(msg);
    }

    /**
     * This is not called on the UI thread. Post a runnable to invoke
     * loadUrl on the UI thread.
     */
    /*public void clickOnAndroid() {
        mHandler.post(new Runnable() {
            public void run() {
                //TODO
            }
        });*/
}
